package com.green.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.green.business.domain.*;
import com.green.business.mapper.SalesMapper;
import com.green.business.service.CustomerService;
import com.green.business.service.GoodsService;
import com.green.business.service.MemberService;
import com.green.business.vo.SalesVo;
import com.green.system.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.business.domain.Sales;
import com.green.business.service.SalesService;
/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/11/19/10:45
 * @Description: 
 */


@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService{


    @Autowired
    private SalesMapper salesMapper ;

    @Autowired
    private MemberService memberService;

    @Autowired
    private GoodsService goodsService;

    @Override
    public DataGridView queryAllSales(SalesVo salesVo) {

        IPage<Sales> page = new Page<>(salesVo.getPage(),salesVo.getLimit());
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(salesVo.getProjectid()!=null,"projectid",salesVo.getProjectid());
        queryWrapper.orderByDesc("number");

        this.salesMapper.selectPage(page,queryWrapper);
        List<Sales> records = page.getRecords();
        for (Sales record : records) {
            if (null!=record.getGoodsid()){
                Goods good = this.goodsService.getById(record.getGoodsid());
                record.setGoodsname(good.getGoodsname());
                record.setSize(good.getSize());
            }
            if (null!=record.getCustomerid()){
                if (record.getCustomerid() == 0){
                    record.setCustomername("非会员用户");
                }else {
                    Member member = this.memberService.getById(record.getCustomerid());
                    record.setCustomername(member.getName());
                }
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    @Override
    public Sales saveSales(Sales sales) {
        this.salesMapper.insert(sales);

        //更新库存
        Integer goodsId = sales.getGoodsid();
        Goods goods = this.goodsService.getById(goodsId);
        goods.setNumber(goods.getNumber()-sales.getNumber());
        this.goodsService.updateGoods(goods);
        return sales;
    }

    @Override
    public Integer queryMaxProjectById() {
        return this.salesMapper.queryMaxProjectById();
    }

    @Override
    public Integer querySalesNumberById(Integer goodsid) {
        Integer a = this.salesMapper.querySalesNumberById(goodsid);
        if (a == null || a.equals("")){
            return 0 ;
        }
        return a;
    }

    @Override
    public Sales updateSales(Sales sales) {
        Sales oldObj = this.salesMapper.selectById(sales.getId());
        Goods goods = this.goodsService.getById(oldObj.getGoodsid());
        goods.setNumber(goods.getNumber()+oldObj.getNumber()-sales.getNumber());
        this.goodsService.updateGoods(goods);
        this.salesMapper.updateById(sales);
        return sales;
    }

    @Override
    public boolean removeById(Serializable id) {
        Sales sales = this.salesMapper.selectById(id);
        Goods goods = this.goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+sales.getNumber());
        this.goodsService.updateGoods(goods);
        return super.removeById(id);
    }
    
}
